/* Selection.h
Copyright (c) 2008 Michael Zahniser
Please note the license terms (MIT license) at the end of this document.

Header file for the Selection class, which represents a selected range of indices.
A selection consists of and "anchor" index (the first item selected) and a "cursor"
index which is the point to which the selection has been expanded. For convenience,
the preincrement and predecrement operators are defined to shift the anchor (which
also resets the selected range to a single index); the postincrement and
postdecrement operators move the cursor without moving the anchor.

For operations that only make sense on a single index, the cursor position is taken
to be the selected index.
*/

#ifndef __MZ_SELECTION_H__
#define __MZ_SELECTION_H__



class Selection {
public:
	// Constructors.
	Selection() : anchor(-1), offset(0) {}
	Selection(int index) : anchor(index), offset(0) {}
	Selection(int anchor, int offset) : anchor(anchor), offset(offset) {}
	Selection(const Selection &s) : anchor(s.anchor), offset(s.offset) {}
	
	// Set the selection.
	void SetFirstAndLast(int first, int last) { anchor = last; offset = first - last; }
	void SetAnchorAndOffset(int anchor, int offset) { this->anchor = anchor; this->offset = offset; }
	void SetCursor(int cursor) { offset = cursor - anchor; }
	
	// Assignment to an index or a selection range.
	Selection &operator=(int index) { anchor = index; offset = 0; return *this; }
	Selection &operator=(const Selection &s) { anchor = s.anchor; offset = s.offset; return *this; }
	
	// Preincrement shifts the anchor.
	void operator--() { anchor += offset; --anchor; offset = 0; }
	void operator++() { anchor += offset; ++anchor; offset = 0; }
	// Postincrement shifts the cursor.
	void operator--(int) { --offset; }
	void operator++(int) { ++offset; }
	// Shift the selection by any integral amount.
	void operator-=(int delta) { anchor = Cursor() - delta; offset = 0;}
	void operator+=(int delta) { anchor = Cursor() + delta; offset = 0;}
	// Get the "canonical" index for this selection.
	operator int() const { return Cursor(); }

	// Accessors.
	int First() const { return anchor + offset * (offset < 0); }
	int Last() const { return anchor + offset * (offset > 0); }
	int Anchor() const { return anchor; }
	int Cursor() const { return anchor + offset; }
	int Offset() const { return offset; }
	int Count() const { return (offset < 0) ? 1 - offset : 1 + offset; }
	bool IsSingle() const { return !offset; }
	
	// Check if this selection contains the given index.
	bool Contains(int index) const { return (index >= First()) & (index <= Last()); }


private:
	// The selection includes all indices between (anchor) and (anchor + offset).
	int anchor;
	int offset;
};



#endif

/* Copyright (c) 2008 Michael Zahniser

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE. */
